package frsf.cidisi.exercise.tpidemia.search.actions;

import frsf.cidisi.exercise.tpidemia.entidades.Cofre;
import frsf.cidisi.exercise.tpidemia.search.*;
import frsf.cidisi.faia.agent.search.SearchAction;
import frsf.cidisi.faia.agent.search.SearchBasedAgentState;
import frsf.cidisi.faia.state.AgentState;
import frsf.cidisi.faia.state.EnvironmentState;

public class TomarTesoro extends SearchAction {

    /**
     * This method updates a tree node state when the search process is running.
     * It does not updates the real world state.
     */
    @Override
    public SearchBasedAgentState execute(SearchBasedAgentState s) {
    	
        ArqueologoState agState = (ArqueologoState) s;
        Cofre tesoroAca = new Cofre();
        tesoroAca = agState.getCofre(agState.getPosicion());
//        System.out.println("\nTesorotomar peso: "+tesoroAca.getPeso());
//        System.out.println("\nTesorotomar valor: "+tesoroAca.getValor());
        if(tesoroAca==null){
        	
        	return null;
        }
        if(tesoroAca.getPeso() < (ArqueologoState.getCapacidad()- agState.getCapacidadOcupada())){
        	agState.setCapacidadOcupada(agState.getCapacidadOcupada()+tesoroAca.getPeso());
        	agState.setValorObtenido(agState.getValorObtenido()+tesoroAca.getValor());
        	agState.getTesoro().remove(agState.getPosicion());
        	
        	
        	
        	//System.out.println(agState.getTesoro().toString());
        	
        	return agState;
        }
        else
        return null;
    }

    /**
     * This method updates the agent state and the real world state.
     */
    @Override
    public EnvironmentState execute(AgentState ast, EnvironmentState est) {
    	
        ArqueologoEstadoEntorno environmentState = (ArqueologoEstadoEntorno) est;
        ArqueologoState agState = ((ArqueologoState) ast);

        Cofre tesoroAca = new Cofre(); 
        tesoroAca = environmentState.getTesoro(agState.getPosicion()).clonarCofre();
        if(tesoroAca==null){
        	return environmentState;
        }
        if(tesoroAca.getPeso() < (ArqueologoState.getCapacidad()- agState.getCapacidadOcupada())){
        	//Actualizamos el Agente
        	agState.setCapacidadOcupada(agState.getCapacidadOcupada()+tesoroAca.getPeso());
        	agState.setValorObtenido(agState.getValorObtenido()+tesoroAca.getValor());
        	
        	//Actualizamos el entorno
        	environmentState.getTesoro().remove(agState.getPosicion());
        	
        	return environmentState;
        }
        
        return environmentState;
    }

    /**
     * This method returns the action cost.
     */
    @Override
    public Double getCost() {
        return new Double(0);
    }

    /**
     * This method is not important for a search based agent, but is essensial
     * when creating a calculus based one.
     */
    @Override
    public String toString() {
        return "Tomar Tesoro";
    }
}